import { Button } from 'antd';
import React, { useState } from 'react';
// 自定义hook：该值只修改要修改的key=>节俭版
function useCustomHookState (initVal) {
    const [state, setState] = useState(initVal);
    const setData = value => {
        setState({ ...state, ...value })
    }
    return [state, setData]
}
const CustomHook = () => {
    const [state, setState] = useState({
        upNumber: 10,
        downNumber: 5,
    });

    const [state1, setState1] = useCustomHookState({
        upNumber: 1,
        downNumber: 2,
    });



    const isUp = (type) => {
        if (type === 'up') {
            // 必须所有的key都赋值上才可以：{...state, upNumber: state.upNumber + 1 }
            // setState({...state, upNumber: state.upNumber + 1 })
            setState1({ upNumber: state1.upNumber + 1 })
        } else {
            // setState({...state, downNumber: state.downNumber + 1 })
            setState1({ downNumber: state1.downNumber + 1 })
        }
    }
    return (
        <div>
            <p>支持:{state1.upNumber}</p>
            <p> 不支持:{state1.downNumber}</p>
            <Button type='primary' onClick={() => isUp('up')}>支持</Button>
            <Button type='primary' danger onClick={() => isUp('down')}>反对</Button>
        </div>
    );
}

export default CustomHook
